//
//  Controller22.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/21.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

//3D touch Quick
class Controller22: UIViewController {

    var isStretch = false
    var redView : UIView!
    var button : UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .blue
        let label = UILabel(frame: CGRect(x: 20, y: 80, width: SCREEN_W - 40, height: 80))
        label.textColor = .black
        view.addSubview(label)
        label.text = "见appdelegate 和 info plist \n so play other things snapkit layout update"
        config();
        // Do any additional setup after loading the view.
    }

    func config(){
        // start y = 160 + 20
        button = UIButton()
        button.setTitle("triggle", for: .normal)
        button.setTitleColor(.purple, for: .normal)
        button.addTarget(self, action: #selector(playAction(sender:)), for: .touchUpInside)
        button.setBackgroundImage(UIImage.creatWithColor(UIColor(white: 0.8, alpha: 1)), for: .normal)
        button.setBackgroundImage(UIImage.creatWithColor(UIColor(white: 0.2, alpha: 1)), for: .selected)
        view.addSubview(button)
        button.snp.makeConstraints { (maker) in
            maker.width.equalTo(80)
            maker.height.equalTo(40)
            maker.centerX.equalTo(self.view.snp.centerX)
            maker.top.equalTo(180)
        }
        
        redView = UIView()
        view.addSubview(redView)
        redView.backgroundColor = .red
        redView.snp.makeConstraints { (maker) in
            maker.centerX.equalTo(button.snp.centerX)
            maker.top.equalTo(button.snp.bottom).offset(10)
            maker.width.equalTo(10)
            maker.height.equalTo(50)
        }
        
    }
    
    override func updateViewConstraints() {
        redView.snp.updateConstraints { (maker) in
            maker.centerX.equalTo(button.snp.centerX)
            maker.top.equalTo(button.snp.bottom).offset(10)
            maker.width.equalTo(10)
            maker.height.equalTo( isStretch ? 150 : 50 )
        }
        super.updateViewConstraints()
    }
    
    func playAction(sender:UIButton){
        isStretch = !isStretch
        view.setNeedsUpdateConstraints()
        view.updateConstraintsIfNeeded()
        UIView.animate(withDuration: 1) {
            self.view.layoutIfNeeded()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
